package controllers;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import supers.GUIController;
import views.SisNotGUI;

public class SisNotController extends GUIController{
	
	private SisNotGUI GUI;
	private boolean repetir = true;
	private boolean MoverTela;

	public SisNotController(SisDskController sisDskController) {
		super(sisDskController, SisNotController.class.getName());
		GUI = (SisNotGUI) super.configGUI(new SisNotGUI(), new GUIListener(), new GUIListener(), new GUIListener());
		GUI.addMouseListener(new GUIListener());
		 EventQueue.invokeLater(new Runnable() {
		      public void run() {
				GUI.setVisible(true);
				verificarNotifs();
				setMoverTela(true);
				new ThreadNotif().start();
		      }
		 });
	}

	protected void verificarNotifs() {
		HashMap<String, Object> notifs = sisDskController.notificacoes.notifs;
		Iterator<String> it = notifs.keySet().iterator();
		String notTit;
		while(it.hasNext()){
		 	notTit = it.next();
			Object objNota = notifs.get(notTit);
			if((objNota instanceof String)&&(((String) objNota).trim().isEmpty() == false)){
				GUI.addTitulo(notTit);
				GUI.addNota((String) objNota);
			}
			if((objNota instanceof ArrayList<?>)&&(((ArrayList<?>) objNota).size() > 0)){
				GUI.addTitulo(notTit);
				if(notTit.split(" ")[0].equals("Contas"))
					GUI.setCorNotas(Color.RED);
				for (Object nota : (ArrayList<?>) objNota) {
					GUI.addNota((String) nota);					
				}
				GUI.setCorNotas(Color.BLACK);
			}
		}
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean validado() {
		// TODO Auto-generated method stub
		return false;
	}

	public void setMoverTela(boolean moverTela) {
		MoverTela = moverTela;
	}

	class ThreadNotif extends Thread{

		@Override
		public void run() {
			while (repetir){				
				while((GUI.pnNotif.getBounds().y * -1) < GUI.pnNotif.getHeight()){
					while(!MoverTela){
						synchronized (this) {
							try {
								wait(250);
							} catch (Exception e) {
							}
						}
					}
					Rectangle bounds = GUI.pnNotif.getBounds();
					bounds.y -= 1;
					GUI.pnNotif.setBounds(bounds);
					synchronized (this) {
						try {
							wait(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
				Rectangle bounds = GUI.pnNotif.getBounds();
				bounds.y = GUI.getSize().height-20;
				GUI.pnNotif.setBounds(bounds);
				//repetir--;
			}
		}
		
	}
	
	class GUIListener implements ActionListener, FocusListener, MouseListener{

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			System.out.println("entrou");
			setMoverTela(false);
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			System.out.println("saiu");
			setMoverTela(true);
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void focusGained(FocusEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void focusLost(FocusEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}

	
}
